﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace AAA.ResultSet
{
    public class TextResultSet : AbstractResultSet
    {
        private string _strFilename;
        private char _cDelimater;
        private bool _hasHeader;
        private int _iSkipLine;

        public TextResultSet(string strFilename)
            : this(strFilename, ',', true)
        {
        }

        public TextResultSet(string strFilename, char cDelimater)
            : this(strFilename, cDelimater, true)
        {
        }

        public TextResultSet(string strFilename, bool hasHeader)
            : this(strFilename, ',', hasHeader)
        {
        }

        public TextResultSet(string strFilename, char cDelimater, bool hasHeader) 
            : this(strFilename, cDelimater, hasHeader, 0)
        {
        }

        public TextResultSet(string strFilename, char cDelimater, bool hasHeader, int iSkipLine)
        {
            _strFilename = strFilename;
            _cDelimater = cDelimater;
            _hasHeader = hasHeader;
            _iSkipLine = iSkipLine;
        }

        public override bool Load()
        {
            StreamReader sr = null;
            string[] strValues;
            string strLine;
            bool isFirstLine;
            try
            {
                if (Encoding == null)
                    sr = new StreamReader(_strFilename);
                else
                    sr = new StreamReader(_strFilename, Encoding);
                //sr = new StreamReader(_strFilename);
                isFirstLine = true;

                for (int i = 0; i < _iSkipLine; i++)
                    sr.ReadLine();

                while ((strLine = sr.ReadLine()) != null)
                {
                    if (IsDoEvents)
                        Application.DoEvents();

                    strValues = strLine.Split(_cDelimater);

                    if (isFirstLine)
                    {
                        if (_hasHeader)
                        {
                            for (int i = 0; i < strValues.Length; i++)
                                AddColumn(strValues[i].Trim());
                            isFirstLine = false;
                            continue;
                        }
                        else
                        {
                            for (int i = 0; i < strValues.Length; i++)
                                AddColumn("Column" + i);
                        }
                    }

                    AddRow(strValues);
                    isFirstLine = false;
                }

                IsSuccess = true;
            }
            catch (Exception ex)
            {
                IsSuccess = false;
                ErrorMessage = ex.Message + "," + ex.StackTrace;
            }
            finally
            {
                if (sr != null)
                    sr.Close();
            }
            return IsSuccess;
        }
    }
}
